﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace NetworkCrashSimulator
{
    class Program
    {

        static void Main(string[] args)
        {
            Header();

            CrawlArgs(args);

            Footer(0);
        }

        static void Header()
        {
            Console.WriteLine(string.Empty);
            Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            Console.WriteLine("Network Crash Simulator " + version.Major.ToString() + "." + version.Minor.ToString());
        }

        static void Footer(int exitCode)
        {
            Console.WriteLine(string.Empty);
            Console.WriteLine("Application ended.");
            Environment.Exit(exitCode);
        }

        static void CrawlArgs(string[] args)
        {
            if (args.Count() > 0)
            {

                if (args[0].Equals("-ListAdapters"))
                {
                    Core.Actions.ListAdapters();
                }
                else if (args[0].Equals("-Crash"))
                {
                    if (args.Count() == 2)
                    {
                        Core.Actions.CrashAdapter(GetAdapterName(args[1]));
                    }
                    else
                    {
                        GenerateArgumentError("Invalid arguments where passed.");
                    }
                }
                else if (args[0].Equals("-Repair"))
                {
                    if (args.Count() == 2)
                    {
                        Core.Actions.RepairAdapter(GetAdapterName(args[1]));
                    }
                    else
                    {
                        GenerateArgumentError("Invalid arguments where passed.");
                    }

                }
                else if (args[0].Equals("-Timed"))
                {
                    if (args.Count() == 3)
                    {
                        Core.TimedCrash timedCrash = new Core.TimedCrash();

                        List<int> timers = GetTimers(args[2]);
                        string adapterName = GetAdapterName(args[1]);

                        timedCrash.Execute(adapterName, timers[0], timers[1], timers[2]);
                    }
                    else
                    {
                        GenerateArgumentError("Invalid arguments where passed.");
                    }

                }
                else if (args[0].Equals("-Help"))
                {
                    Help.ShowFull();
                }
                else
                {
                    GenerateArgumentError("Invalid arguments where passed.");
                }
            }
            else
            {
                GenerateArgumentError("No arguments where passed.");
            }
        }

        static string GetAdapterName(string arg)
        {
            string ret = string.Empty; ;

            if (arg.Contains("-Adapter="))
            {
                ret = arg.Replace("-Adapter=", "");
            }
            else
            {
                GenerateArgumentError("The argument -Adapter where not passed.");
            }

            return ret;
        }

        static List<int> GetTimers(string arg)
        {
            List<int> ret = new List<int>();

            if (arg.Contains("-Timers="))
            {
                try
                {
                    string timer = arg.Replace("-Timers=", "");
                    string[] timers = timer.Split(Convert.ToChar(","));

                    foreach (string value in timers)
                    {
                        ret.Add(Convert.ToInt32(value));
                    }
                }
                catch
                {
                    GenerateArgumentError("The argument -Timers is not well formed.");
                }
            }
            else
            {
                GenerateArgumentError("The argument -Timers where not passed.");
            }

            return ret;
        }

        static void GenerateArgumentError(string message)
        {
            Console.WriteLine(string.Empty);
            Console.WriteLine("ERROR: " + message);
            Console.WriteLine(string.Empty);
            Console.WriteLine("Try using the -Help argument to see the Help.");
            Footer(1);
        }


    }
}
